package ldh.im.web.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import ldh.common.PageResult;
import ldh.im.web.dao.AuthorityDao;
import ldh.im.web.pojo.Authority;
import ldh.im.web.pojo.where.AuthorityWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: ldh
 * @date: 2019-10-25 12:35:05
 */
@Service("authorityService")
public class AuthorityService {

	@Resource
	private AuthorityDao authorityDao;

	@Transactional
	public Authority insert(Authority authority) {
		int n = authorityDao.insert(authority);
		if (n != 1) {
			throw new RuntimeException("Authority insert error!");
		}
		return authority;
	}

	@Transactional
	public Authority insertSelective(Authority authority) {
		int n = authorityDao.insert(authority);
		if (n != 1) {
			throw new RuntimeException("Authority insertSelective error!");
		}
		return authority;
	}

	@Transactional
	public Authority updateById(Authority authority) {
		int  n = authorityDao.updateById(authority);
		if (n != 1) {
			throw new RuntimeException("Authority update error!");
		}
		return authority;
	}

	@Transactional
	public Authority updateNotNullById(Authority authority) {
		int  n = authorityDao.updateNotNullById(authority);
		if (n != 1) {
			throw new RuntimeException("Authority update error!");
		}
		return authority;
	}


	@Transactional
	public void deleteById(Long id) {
		int n = authorityDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("Authority delete error!");
		}
	}

	@Transactional(readOnly = true)
	public Authority getById(Long id) {
		Authority authority = authorityDao.getById(id);
		return authority;
	}

	@Transactional(readOnly = true)
	public Authority getManyRolesById(Long id) {
		Authority authority = authorityDao.getManyRolesById(id);
		return authority;
	}

	@Transactional(readOnly = true)
	public List<Authority> getByIds(List<Long> ids) {
		if (ids.size() == 0) return new ArrayList<>();
		return authorityDao.getByIds(ids);
	}

	@Transactional(readOnly = true)
	public Authority getByNameAndModuleId(String name) {
		Authority authority = authorityDao.getByName(name);
		return authority;
	}

	@Transactional(readOnly = true)
	public List<Authority> getAllAuthoritys(AuthorityWhere authorityWhere) {
		authorityWhere.setPageSize(Integer.MAX_VALUE);
		return authorityDao.findByAuthorityWhere(authorityWhere);
	}

	@Transactional(readOnly = true)
	public PageResult<Authority> findByAuthorityWhere(AuthorityWhere authorityWhere) {
		long total = authorityDao.findTotalByAuthorityWhere(authorityWhere);
		List<Authority> imageList = new ArrayList<Authority>();
		if (total > 0) {
			imageList  = authorityDao.findByAuthorityWhere(authorityWhere);
		}

		return new PageResult<Authority>(authorityWhere, total, imageList);
	}

	@Transactional(readOnly = true)
	public long findTotalByAuthorityWhere(AuthorityWhere authorityWhere) {
		long total = authorityDao.findTotalByAuthorityWhere(authorityWhere);
		return total;
	}

	@Transactional(readOnly = true)
	public PageResult<Authority> findJoinByAuthorityWhere(AuthorityWhere authorityWhere) {
		long total = authorityDao.findJoinTotalByAuthorityWhere(authorityWhere);
		List<Authority> imageList = new ArrayList<Authority>();
		if (total > 0) {
			imageList  = authorityDao.findJoinByAuthorityWhere(authorityWhere);
		}

		return new PageResult<Authority>(authorityWhere, total, imageList);
	}
}
